package com.test.spring;

import com.example.entity.CarEntity;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    /**
     * 同样是在bean定义之前的处理器，执行顺序在beanfactorypostprocessor之前，这个接口还实现了这个接口
     * @param beanDefinitionRegistry
     * @throws BeansException
     */
    //是在Bean定义全部加载完毕前并且可以自己添加bean去注册等
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        System.out.println("444444444444444");
        RootBeanDefinition helloBean = new RootBeanDefinition(CarEntity.class);
        //新增Bean定义
        beanDefinitionRegistry.registerBeanDefinition("hello", helloBean);
    }
    //是在Bean定义全部加载完毕后且Bean实例化之前通过postProcessBeanFactory方法
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("33333333333");
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("hello");
        MutablePropertyValues beanProperty = beanDefinition.getPropertyValues();
        beanProperty.add("id",12345L);
        System.out.println(beanProperty);
    }
}
